<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\FhNews;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
class FhNewsController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new FhNews(), function (Grid $grid) {
            $grid->model()->orderBy('id','desc');
            $grid->column('id')->sortable();
            $grid->column('title');
            $grid->column('url');
            $grid->column('type')->using([1=>"行业资讯",2=>"峰华资讯",3=>"技术资讯"]);
            $grid->column('img')->image("",100,40);
            $grid->column('hot')->switch();
            $grid->column('show')->switch();
            // $grid->column('nums')->editable();
            $grid->column('created_at');
            $grid->selector(function (Grid\Tools\Selector $selector) {//该部分为筛选模块
                $selector->select('hot', '是否热门', ['0' => '否', '1' => '是']);
                $selector->select('type', '分类', ['1' => '行业资讯', '2'=>'峰华资讯',3=>"技术资讯"]);
            });
            $grid->actions(function ($actions) {
                // 隐藏显示按钮
                $actions->disableView();
            });
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
        
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new FhNews(), function (Show $show) {
            $show->field('id');
            $show->field('title');
            $show->field('description');
            $show->field('img');
            $show->field('keyword');
            $show->field('seo_desc');
            $show->field('type');
            $show->field('hot');
            $show->field('show');
            $show->field('content');
            $show->field('nums');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new FhNews(), function (Form $form) {
            $form->display('id');
            $form->text('title')->required();
            $form->text('url')->required()->help("直接输入地址例如 news1 不能输入?&.{}# 空格等特殊字符");
            // $form->text('description')->maxLength(255)->required();
            $form->image('img')->thumbnail('small',300,300)->uniqueName()->move('images/'.date("Ymd",time()));
            $form->hidden('thumb');
            $form->text('keyword')->maxLength(190);
            $form->text('seo_desc')->maxLength(255);
            $form->radio('type')->options([1=>"行业资讯",2=>"峰华资讯",3=>"技术资讯"])->default(1);
            $form->multipleSelect('tags',"标签")->options('/apis/getTag')->saving(function ($value) {
                // 转化成json字符串保存到数据库
                    return implode(',',$value);
            });
            $form->switch('hot');
            $form->switch('show')->default(1);
            $form->editor('content');
            $form->number('nums');

            $form->saving(function (Form $form) {
                $form->model()->id; 
                if (!$form->model()->id) {

                    $pattern = '/[?\&.\{\}#\s]/';  // 正则表达式
                    $isValid = preg_match($pattern,  $form->url) === 1; // 验证逻辑
                    if($isValid){
                        throw new \Exception('路由地址包含 ?&.{}# 空格特殊字符');
                    }
                }
                if($form->img){
                    $thumb=explode(".",$form->img);
                    $form->thumb=$thumb[0].'-small.'.$thumb[1];
                }
            });

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
